package com.jiangyifen.robot.web;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiangyifen.robot.sdk.RobotSdkClient;
import com.jiangyifen.robot.sdk.listener.DefaultRobotEventListener;

@Controller
@RequestMapping("test")
public class TestController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private RobotSdkClient client = new RobotSdkClient();;
	
	@PostConstruct
	public void init() {
		try {
			client.connect("127.0.0.1", 8888, "123456");
			client.addListener(new DefaultRobotEventListener());
			
			client.startupAll(1L);
			client.call(1L, "8003");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@RequestMapping("disconnect")
	@ResponseBody
	public String disconnect(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("startupAll")
	@ResponseBody
	public String startupAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.startupAll(1L);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("call")
	@ResponseBody
	public String call(HttpServletRequest request, HttpServletResponse response, String p) {
		try {
			client.call(1L, p);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("dtmf")
	@ResponseBody
	public String dtmf(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.dtmf(1, '#');
			client.dtmf(1, '8');
			client.dtmf(1, '0');
			client.dtmf(1, '0');
			client.dtmf(1, '9');
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("hangup")
	@ResponseBody
	public String hangup(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.hangup(1);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("sayText")
	@ResponseBody
	public String sayText(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.sayText(1, "机器人说话，卡不卡，抖不抖，机器人说话，卡不卡，抖不抖", true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}
	
	@RequestMapping("sayUrl")
	@ResponseBody
	public String sayUrl(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.sayUrl(1, "http://116.62.68.198:18080/vmw/assets/170020.wav", true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

	@RequestMapping("sayFile")
	@ResponseBody
	public String sayFile(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.sayFile(1, "170020.wav", false);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";

	}

	@RequestMapping("stopTalking")
	@ResponseBody
	public String stopTalking(HttpServletRequest request, HttpServletResponse response) {
		try {
			client.stopTalking(1);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	}

}
